package com.hehanpeng.springbootnetty.test;

import java.lang.reflect.Field;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class ISO8583Util {
	
	
	@SuppressWarnings("unchecked")
	public static ISO8583Bean parse(Map<String, Object> map) throws Exception {
		ISO8583Bean bean = new ISO8583Bean();
		
		Field[] fields = ISO8583Bean.class.getDeclaredFields();
		Set<Entry<String, Object>> entry = map.entrySet();
		for(Entry<String, Object> obj : entry) {
			String key = obj.getKey();
			Map<String, String> valueMap = (Map<String, String>) obj.getValue();
			
			for(int i=0;i<fields.length;i++) {
				Field field = ISO8583Bean.class.getDeclaredField(fields[i].getName());
				field.setAccessible(true);
				Field8583 field8583 = field.getAnnotation(Field8583.class);
				
				if(field8583.bitMap().equals(key)) {
					field.set(bean, valueMap.get("value"));
				}
			}
		}
		
		return bean;
	}

}
